
import { UrlService } from './../../providers/url-service';
import { Component } from '@angular/core';
import { NavController, NavParams, ToastController, LoadingController, Events, ViewController } from 'ionic-angular';
import { Keyboard } from '@ionic-native/keyboard';
import { Api } from '../../providers/api-service';
import { TabsPage } from '../tabs/tabs';
import { RegisterPage } from '../login/register/register';
import { PasswordResetPage } from '../login/password-reset/password-reset';
import { Storage } from '@ionic/storage/dist';
import { LoginPluginProvider } from '../../providers/login-plugin/login-plugin';
import { QQSDK, QQShareOptions } from '@ionic-native/qqsdk';
import { STORE_HAS_GUIDE_KEY } from '../../config/constants';

@Component({
	selector: 'page-login',
	templateUrl: 'login.html'
})
export class LoginPage {
	[x: string]: any;
	mobile;
	password;
	loading;
	loginReason;
	mobileIsOk = true;
	isNotMobileWeb = true;
	constructor(
		public storage: Storage,
		public navCtrl: NavController,
		public navParams: NavParams,
		public api: Api,
		public loginPlugin: LoginPluginProvider,
		public toastCtrl: ToastController,
		public loadingCtrl: LoadingController,
		public events: Events,
		public url: UrlService,
		public view: ViewController,
		public keyboard: Keyboard,
		public qqSDK: QQSDK

	) {
		if (!this.api.isMobileWeb()) {
			this.isNotMobileWeb = true;
		}
	}

	ionViewDidLoad() {
		this.loginReason = this.navParams.get('loginReason');

	}

	// // 页面即将被卸载掉,清理所有定时任务
	// ionViewWillUnload() {

	// }


	/**
 * 去注册
 */
	goRegister() {
		this.navCtrl.push(RegisterPage);
	}
	/**
 * 忘记密码  找回密码
 */
	goResetPassword() {
		this.keyboard.close();
		this.navCtrl.push(PasswordResetPage);
	}

	// 登录
	loginNow() {
		this.loading = this.loadingCtrl.create({
			content: '登陆中...'
		});
		this.loading.present();
		this.keyboard.close();
		//
		this.url.login(this.mobile, this.password)
			.then((res) => {
				this.loginSucess(res);
			})
			.catch((err) => {
				this.loading.dismiss();
				this.api.showTip(err.Msg);
			});
	}
	/**
	 * 更多登录
	 * @param type 
	 */
	loginBtn(type) {
		switch (type) {
			case 1:
				this.loginByQQ();
				break;
			case 2:
				this.loginByWechat();
				break;
			case 3:
				this.loginBySina();
				break;
		}
	}

	// 第三方登陆分三步走
	// - 1. 调起第三方登陆,获取第三方的数据.微信的code , qq的access_token => loginByWechat() or loginByQQ()
	// - 2. 仅仅qq ,通过user/auth接口,获取openid和access_token => getOpenidBySocial() ; 微信直接跳到下一步
	// - 3. 调快码的登陆接口,传入数据,返回看是否有uid选择是跳首页还是绑定页 => loginBySocail()
	// 微信登陆相关
	loginByWechat() {
		// 非正常的取消登陆,并不会触发then和catch
		this.loginPlugin.loginByWechat()
			.then(result => {
				this.api.postBody("V1/LoginByOther", { "Type": 2, "OtherLog": result['openid'] })
					.then(data => {
						this.loginSucess(data);
					})
			})
			.catch(() => { })
	}

	/**
	 * 微博登录
	 */
	loginBySina() {
		this.loginPlugin.loginBySina()
			.then((result) => {
				this.api.postBody("V1/LoginByOther", { "Type": 3, "OtherLog": result['openid'] })
					.then(data => {
						this.loginSucess(data);
					})
			})
			.catch(() => { })
	}

	clientOptions: QQShareOptions = {
		client: this.qqSDK.ClientType.QQ,
	};
	/**
	 * QQ登录
	 */
	loginByQQ() {
		//检测设备上是否安装了QQ应用程序或TIM应用程序。
		this.qqSDK.checkClientInstalled(this.clientOptions)
			.then(() => {
				this.qqSDK.ssoLogin(this.clientOptions)
					.then((result) => {
						this.api.postBody("V1/LoginByOther", { "Type": 1, "OtherLog": result.userid })
							.then(data => {
								this.loginSucess(data);
							})
					})
					.catch((error) => {
						this.loginPlugin.Toast("QQ登录失败");
					});
			})
			.catch(() => {
				this.api.showNoticeByToast(1, "未检测到QQ手机应用");
			});
	}
	/**
	 * 登录处理
	 * @param data 
	 */
	private loginSucess(data) {

		if (data.Code === 0) {
			this.api.writeLocalStorage(STORE_HAS_GUIDE_KEY, true);
			let obj = data.Data;
			//存入本地
			localStorage.setItem('Ticket', obj.LoginTicket);
			localStorage.setItem('User', JSON.stringify(obj.User));
			this.storage.set('firstIn', 2);//设置启动参数
			this.loading.dismiss();
			this.navCtrl.push(TabsPage);
		} else {
			this.loading.dismiss();
			this.api.showTip(data.Msg);
		}
	}
	chakanPwd = true;
	/**
	 * 查看密码
	 */
	chaPwd() {
		this.chakanPwd = !this.chakanPwd;
	}
}
